// pages/component/deviceItem/deviceItem.js
const app = getApp();

const api = require('../../../apis/request.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    device: {
      type: Object,
      value: undefined
    },
    controlAble: {
      type: Boolean,
      value: true
    },
    device_list: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    doubleItems: [{
        value: '00',
        name: '反转'
      },
      {
        value: '64',
        name: '正转'
      },
      {
        value: 'AA',
        name: '停止'
      }
    ],
    singleItems: [{
        value: '00',
        name: '关'
      },
      {
        value: '64',
        name: '开'
      }

    ],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    singleCtrl: function(event) {
      // console.log(event.currentTarget.dataset,'单路设备')

      // console.log('singleCtrl', event.detail.value);
      var _this = this;
      // var device = this.data.device
      this.data.device_list.some(item => {
        if (!_this.data.controlAble) {
          _this.setData({
            singleItems: _this.data.singleItems
          });
         
          return;
        }
      // console.log(device)
        if (item.id == event.currentTarget.dataset.bid) {
          // continue;
          console.log(item)
          api.singleCtrl(item.gSerial,
            item.controllerAddr,
            item.deviceAddr,
            item.isDoubleSwitch,
            event.detail.value,
            function (res) {
              var data = res.data;
              // console.log(JSON.stringify(data));
              // _this.setData({url:data.datas[0].playUrl});
              // var cameraList = data.datas;
              // _this.setData({ cameraList: cameraList });
              if (data.status != 0) {
                wx.showToast({
                  title: '设备不在线'


                });
                return;
              }
              wx.showToast({
                title: data.msg
              });
              //状态改变
              var items = _this.data.singleItems;
              for (var i = 0; i < items.length; ++i) {
                items[i].checked = items[i].value == event.detail.value
              };
              // console.log(items);

              _this.setData({
                singleItems: items
              });
              // console.log("_this1 = " + _this);
              // _this.setData({ deviceList: data.datas });
            });
          return true

        }
      

      })


    },
    doubleCtrl: function(event) {

      // console.log('doubleCtrl', event.detail.value, event.currentTarget.dataset.bid);
      var _this = this;
      this.data.device_list.forEach(item => {
        if (!_this.data.controlAble) {
          _this.setData({
            doubleItems: _this.data.doubleItems
          });
          return;
        }

        if (item.id == event.currentTarget.dataset.bid) {
          console.log(item)

        api.doubleCtrl(item.gSerial,
          item.controllerAddr,
          item.deviceAddr,
          item.isDoubleSwitch,
          event.detail.value,
          function(res) {
            // 00
            // 反转
            // 64
            // 正转
            // AA
            // 停止
            var data = res.data;
            console.log(data)
            // if (data.data.curStatu == '00') {
            //   item.iconPath_x = item.iconPath_gif
            //   console.log(item)

            // } else if (data.data.curStatu == '64') {
            //   item.iconPath_x = item.iconPath_gif
            //   console.log(item)
            // } else if (data.data.curStatu == 'AA') {
            //   item.iconPath_x = item.iconPath

            // }
            // console.log(JSON.stringify(data));
            // _this.setData({url:data.datas[0].playUrl});
            // var cameraList = data.datas;
            // _this.setData({ cameraList: cameraList });
            if (data.status != 0) {

              wx.showToast({
                title: '设备不在线'
              });
              return;
            }
            wx.showToast({
              title: data.msg
            });
            var items = _this.data.doubleItems;
            for (var i = 0; i < items.length; ++i) {
              items[i].checked = items[i].value == event.detail.value
            };
            // console.log(items);

            _this.setData({
              doubleItems: items
            });

            // console.log("_this1 = " + _this);
            // _this.setData({ deviceList: data.datas });
          });
          return true
        }

      })
    },
    setTiming: function(event) {
      var _this = this;
      var e_id = event.target.dataset.e_id
      // console.log(event.target.dataset.e_id)

      wx.navigateTo({
        url: '../timing/timing?data=' + e_id
      });
      // console.log(e_id)
    },
    // 设备图标
    device_icon(a) {
      // console.log(a,'点击传值')
      var _this = this;
      var device
      // console.log("name:" + item.name);
      this.data.device_list.forEach(item => {
        device = item;
        // console.log(device, '预传值')

        if (device.isDoubleSwitch) {
          var items = _this.data.doubleItems;
          for (var i = 0; i < items.length; ++i) {
            items[i].checked = items[i].value == device.curStatu
          };
          // console.log(items);

          _this.setData({
            doubleItems: items
          });
        } else {
          var items = _this.data.singleItems;
          for (var i = 0; i < items.length; ++i) {
            items[i].checked = items[i].value == device.curStatu
          };
          // console.log(items);

          _this.setData({
            singleItems: items

          });
        }
      })

      // this.iconPath_method(device)

    },
    // iconPath_method(device) {
    //   // console.log(device,132132132112)
    //   var _this = this
    //   var iconPath = "../../../img/device/d_fan.png";
    //   console.log(device.deviceType)
    //   switch (device.deviceType) {
    //     case '00':
    //       iconPath = "../../../img/device/d_fan.png";
    //       break;
    //     case '01':

    //       iconPath = "../../../img/device/d_cycle_fan.png";
    //       break;
    //     case '02':
    //       iconPath = "../../../img/device/d_sky_window.png";
    //       break;
    //     case '03':
    //       iconPath = "../../../img/device/d_side_window.png";
    //       break;
    //     case '04':
    //       iconPath = "../../../img/device/d_out_shadow.png";
    //       break;
    //     case '05':
    //       iconPath = "../../../img/device/d_in_shadow.png";
    //       break;
    //     case '06':
    //       iconPath = "../../../img/device/d_cool_pad.png";
    //       break;
    //     case '07':
    //       iconPath = "../../../img/device/d_water_pump.png";
    //       break;
    //     case '08':
    //       iconPath = "../../../img/device/d_sol_value.png";
    //       break;
    //     case '09':
    //       iconPath = "../../../img/device/d_solenoid_value.png";
    //       break;
    //     case '10':
    //       iconPath = "../../../img/device/d_light.png";
    //       break;
    //     case '11':
    //       iconPath = "../../../img/device/d_heat.png";
    //       break;
    //     case '12':
    //       iconPath = "../../../img/device/d_gas.png";
    //       break;
    //     case '13':
    //       iconPath = "../../../img/device/d_roll_film.png";
    //       break;
    //     case '14':
    //       iconPath = "../../../img/device/d_roll.png";
    //       break;
    //   }
    //   _this.setData({
    //     iconPath: iconPath
    //   });
    // },

  },
  lifetimes: {
    ready: function() {
      this.device_icon()
      // this.data.device_list.forEach
      // app.setWatcher(this.data, this.watch); // 设置监听器
      // this.setData({
      //   name: 'lxm'
      // })



    }
  },
  // watch: {
  //   name: function (newValue) {
  //     console.log(newValue); // name改变时，调用该方法输出新值。
  //   }
  // }

})